<template>
  <div class="image-cropper">
    <el-card class="tool-card">
      <template #header>
        <div class="card-header">
          <h2>图片裁剪</h2>
          <div class="header-actions">
            <!-- 删除使用说明按钮 -->
          </div>
        </div>
      </template>
      
      <!-- 使用指南 -->
      <section class="usage-section">
        <div class="usage-wrapper">
          <div class="usage-title-section">
            <el-icon size="28" class="usage-icon"><Guide /></el-icon>
            <h2>快速使用指南</h2>
          </div>
          <div class="usage-content">
            <div class="guide-steps">
              <div class="step-item">
                <div class="step-number">1</div>
                <div class="step-content">
                  <h4>上传图片</h4>
                  <p>将图片拖拽到上传区域或点击上传</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">2</div>
                <div class="step-content">
                  <h4>调整裁剪</h4>
                  <p>在预览区域调整裁剪框的位置和大小</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">3</div>
                <div class="step-content">
                  <h4>选择比例</h4>
                  <p>选择裁剪比例（自由、正方形、16:9等）</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">4</div>
                <div class="step-content">
                  <h4>获取结果</h4>
                  <p>裁剪完成后预览效果并下载图片</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 上传区域 -->
      <div class="upload-area">
        <el-upload
          class="image-uploader"
          action="#"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="handleFileChange"
          accept="image/jpeg,image/png,image/gif"
          drag
        >
          <el-icon class="upload-icon"><Upload /></el-icon>
          <div class="upload-text">
            <span>将图片拖到此处，或<em>点击上传</em></span>
            <p class="upload-hint">支持JPG、PNG、GIF格式，单个文件不超过5MB</p>
          </div>
        </el-upload>
      </div>
      
      <!-- 编辑区域 -->
      <div v-if="imageUrl" class="edit-area">
        <div class="crop-container">
          <div class="image-container">
            <!-- 这里将引入裁剪组件 -->
            <div class="cropper-placeholder">
              <span>裁剪功能开发中...</span>
            </div>
          </div>
        </div>
        
        <div class="crop-options">
          <div class="aspect-ratio-selector">
            <span class="option-label">裁剪比例：</span>
            <el-radio-group v-model="aspectRatio" size="small">
              <el-radio-button label="free">自由</el-radio-button>
              <el-radio-button label="1:1">1:1</el-radio-button>
              <el-radio-button label="4:3">4:3</el-radio-button>
              <el-radio-button label="16:9">16:9</el-radio-button>
              <el-radio-button label="3:4">3:4</el-radio-button>
            </el-radio-group>
          </div>
          
          <div class="crop-actions">
            <el-button type="primary" @click="cropImage">裁剪并预览</el-button>
            <el-button @click="resetCrop">重置</el-button>
          </div>
        </div>
      </div>
      
      <!-- 裁剪结果预览 -->
      <div v-if="croppedImageUrl" class="preview-area">
        <h3>裁剪结果预览</h3>
        <div class="preview-image">
          <img :src="croppedImageUrl" alt="裁剪后的图片" />
        </div>
        
        <div class="preview-actions">
          <el-button type="success" @click="downloadImage">下载图片</el-button>
          <el-button @click="newCrop">重新裁剪</el-button>
        </div>
      </div>
      
      <!-- 开发中提示 -->
      <el-alert
        title="功能开发中"
        type="warning"
        description="图片裁剪功能正在开发中，即将上线！"
        :closable="false"
        show-icon
        style="margin-top: 20px;"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { Upload, Guide } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';

// 状态变量
const imageUrl = ref('');
const croppedImageUrl = ref('');
const aspectRatio = ref('free');
const router = useRouter();

// 文件上传处理
const handleFileChange = (file) => {
  ElMessage.warning('图片裁剪功能正在开发中，即将上线！');
  
  // 仅做示例，实际功能未实现
  if (file && file.raw) {
    const isImage = file.raw.type.indexOf('image/') !== -1;
    const isLt5M = file.raw.size / 1024 / 1024 < 5;
    
    if (!isImage) {
      ElMessage.error('只能上传图片文件!');
      return false;
    }
    
    if (!isLt5M) {
      ElMessage.error('图片大小不能超过5MB!');
      return false;
    }
    
    // 读取图片并显示
    const reader = new FileReader();
    reader.readAsDataURL(file.raw);
    reader.onload = () => {
      imageUrl.value = reader.result;
    };
  }
};

// 裁剪图片
const cropImage = () => {
  ElMessage.warning('图片裁剪功能正在开发中，即将上线！');
  // 实际功能未实现，这里仅做示例
  croppedImageUrl.value = imageUrl.value;
};

// 重置裁剪
const resetCrop = () => {
  // 实际功能未实现，这里仅做示例
  ElMessage.info('裁剪已重置');
};

// 下载裁剪后的图片
const downloadImage = () => {
  ElMessage.warning('下载功能正在开发中，即将上线！');
  // 实际功能未实现，这里仅做示例
};

// 重新裁剪
const newCrop = () => {
  croppedImageUrl.value = '';
};

// 升级会员
const handleUpgrade = () => {
  // 使用 Vue Router 导航到会员页面，使用命名路由而不是路径
  router.push({ name: 'membership' });
};
</script>

<style scoped>
.image-cropper {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.usage-guide {
  margin-bottom: 20px;
}

.guide-steps {
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 4px;
  margin-top: 10px;
}

.guide-steps ol {
  margin: 0;
  padding-left: 20px;
}

.guide-steps li {
  margin-bottom: 5px;
}

.upload-area {
  margin-bottom: 20px;
}

.upload-text {
  margin-top: 10px;
}

.upload-hint {
  color: #909399;
  font-size: 12px;
  margin-top: 5px;
}

.upload-icon {
  font-size: 30px;
  color: #c0c4cc;
  margin-bottom: 8px;
}

.edit-area {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 20px;
}

.crop-container {
  flex: 2;
  min-width: 400px;
}

.preview-container {
  flex: 1;
  min-width: 300px;
}

.image-container {
  position: relative;
  width: 100%;
  height: 400px;
  background-color: #f5f7fa;
  display: flex;
  justify-content: center;
  align-items: center;
}

.cropper-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  color: #909399;
  font-size: 18px;
  background-color: #f5f7fa;
}

.crop-options {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-top: 15px;
}

.aspect-ratio-selector {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.option-label {
  font-weight: bold;
  color: #606266;
}

.crop-actions {
  display: flex;
  gap: 10px;
  margin-top: 5px;
}

.preview-area {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.preview-area h3 {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 16px;
  color: #303133;
}

.preview-image {
  text-align: center;
  margin-bottom: 15px;
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
}

.preview-image img {
  max-width: 100%;
  max-height: 400px;
}

.preview-actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 15px;
}

.usage-section {
  margin-top: 36px;
  padding: 0 12px;
  margin-bottom: 32px;
}

.usage-wrapper {
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  border: 1px solid #ebeef5;
}

.usage-title-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.usage-icon {
  color: #3498db;
}

.usage-title-section h2 {
  margin: 0;
  color: #303133;
  font-size: 20px;
  font-weight: 600;
}

.usage-content {
  display: flex;
  flex-direction: column;
}

.guide-steps {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.step-item {
  flex: 1;
  min-width: 180px;
  display: flex;
  align-items: flex-start;
  gap: 16px;
  background-color: white;
  padding: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.step-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.step-number {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #3498db;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  flex-shrink: 0;
}

.step-content {
  flex: 1;
}

.step-content h4 {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.step-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .guide-steps {
    flex-direction: column;
  }
}

@media (max-width: 768px) {
  .aspect-ratio-selector {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .crop-actions {
    flex-direction: column;
  }
  
  .preview-actions {
    flex-direction: column;
  }
}
</style> 